<template>
  <el-container class="home-container">
    <el-header>
      <div class="headImage">
        <img
          src="https://images.91160.com/unit/notice/2016082209452967277.jpg"
          alt=""
        />
        <span> 医疗健康管理与产品销售平台</span>
      </div>
      <logout></logout>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <el-row class="tac">
          <el-col :span="12">
            <el-menu
              :default-active="activeIndex"
              class="el-menu-vertical-demo"
              @open="handleOpen"
              @close="handleClose"
              background-color="#ffffff"
              text-color="#333333"
              active-text-color="#2ecc71"
              :unique-opened="true"
              :router="true"
            >
              <template v-for="item in menuList">
                <el-submenu
                  :index="item.id.toString()"
                  v-if="item.children && item.children.length"
                  :key="item.id"
                >
                  <template slot="title">
                    <i :class="item.icon"></i>
                    <span>{{ item.name }}</span>
                  </template>
                  <el-menu-item
                    :index="subItem.path"
                    v-for="subItem in item.children"
                    :key="subItem.id"
                  >
                    <template slot="title">
                      <i :class="subItem.icon"></i>
                      <span>{{ subItem.name }}</span>
                    </template>
                  </el-menu-item>
                </el-submenu>
                <el-menu-item v-else :index="item.path" :key="item.id">
                  <i :class="item.icon"></i>
                  <span>{{ item.name }}</span>
                </el-menu-item>
              </template>
            </el-menu>
          </el-col>
        </el-row>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import Logout from "./Logout.vue";
export default {
  components: { Logout },
  name: "HomeVue",
  data() {
    return {
      menuList: [],
      activeIndex: '/users' 
    };
  },
  methods: {
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
    setActiveIndex() {
      // 获取当前路由路径，并设置为 activeIndex
      this.activeIndex = this.$route.path;
    }
  },
  watch: {
    // 监听路由变化，实时更新 activeIndex
    $route() {
      this.setActiveIndex();
    }
  },
  async created() {
    const result = await this.$http.get("/webPage/common/menu");
    this.menuList = result.data.data;
    console.log(this.menuList);
    
     this.setActiveIndex();
  },
};
</script>

<style lang='less' scoped>
.home-container {
  height: 100%;
  font-family: "Helvetica Neue", Arial, sans-serif;
}

.el-header,
.el-footer {
  background-color: #3498db;
  color: #ffffff;
  font-size: 18px;
  text-align: center;
  line-height: 60px;
  display: flex;
  justify-content: space-between;
  padding: 0 20px;
  .headImage {
    display: flex;
    align-items: center;
    img {
      width: 50px;
      height: 50px;
      border-radius: 50%;
      margin-right: 15px;
    }
    span {
      font-weight: bold;
    }
  }
  logout {
    margin-left: auto;
  }
}

.el-main {
  background-color: #f5f5f5;
  color: #333;
  text-align: center;
  line-height: 62px;
  height: 100%;
  padding: 20px;
}

.el-aside {
  background-color: #ffffff;
  color: #333;
  text-align: center;
  line-height: 200px;
  height: 100%;
  box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
  .tac {
    height: 100%;
  }
}

.el-menu-vertical-demo {
  height: 100%;
  width: 100%;
  text-align: left;
  border-right: none;
}

.el-col {
  height: 100%;
  width: 100%;
}

.el-menu-item,
.el-submenu__title {
  height: 56px;
  line-height: 56px;
  padding: 0 20px;
  font-size: 14px;
  color: #333;
  transition: all 0.3s ease;
}

.el-menu-item i,
.el-submenu__title i {
  color: #999;
  margin-right: 10px;
  font-size: 18px;
}

.el-menu-item:hover,
.el-submenu__title:hover {
  background-color: #e8f5fe;
}

.el-menu-item.is-active {
  background-color: #e8f5fe;
  color: #1e88e5;
  border-right: 3px solid #1e88e5;
}

.el-menu-item.is-active i {
  color: #1e88e5;
}

.el-submenu.is-active .el-submenu__title {
  color: #1e88e5;
}

.el-submenu.is-active .el-submenu__title i {
  color: #1e88e5;
}

.el-submenu__title:hover {
  background-color: #f5f5f5;
}

.el-menu--inline .el-menu-item {
  padding-left: 48px !important;
}
</style>